package edu.api.objects_api;

import java.util.Comparator;
import java.util.Objects;

public class Test {

    public static void main(String[] args) {

        Student student = new Student("2020111", "张三", 20);
        if (null == student) {
            student = new Student("2020111", "张三", 20);
        }
        //利用Objects判断对象是否为空
        if (Objects.isNull(student)) {
            System.out.println("student为null");
        }
        if (Objects.nonNull(student)) {
            System.out.println("student不为null");
        }
        student = null;
        //用于判断参数是否为null,不为null，就返回
//        student = Objects.requireNonNull(student, "当前对象为null");

        int i = Objects.compare(2, 3, new MyComparator());
        System.out.println("i = " + i);
        Student student2 = new Student("2020111", "张三", 20);
        Student student3 = new Student("2020111", "张三", 20);
        //比较规则
        Comparator<Student> comparator = new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge() - o2.getAge();
            }
        };

        int j = Objects.compare(student2, student3, comparator);
        System.out.println("j = " + j);

        // equals  自带判断第一个参数是否为null   可以避免第一个参数为null时出现空指针
        student2 = null;
        boolean b = Objects.equals(student2, student3);
        System.out.println("b = " + b);

    }

}




/**
 * 自定义类实现比较标准    <比较的数据类型>
 */
class MyComparator implements Comparator<Integer> {

    @Override
    public int compare(Integer o1, Integer o2) {
        return o1 - o2;
    }
}

